
package com.brandonflowers.hiveswfaddressdemo.views 
{
	import com.brandonflowers.hiveswfaddressdemo.states.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.text.*;
	import flash.utils.getDefinitionByName;
	
	import org.hive.sequence.*;
	import org.hive.state.*;
	import org.hive.managers.plugin.LayoutManager;
	import org.hive.managers.plugin.LoadManager;
	import org.hive.managers.StateManager;
	import org.hive.state.*
	import org.hive.sequence.*
	import org.hive.data.LoaderData;
	import org.hive.data.LoaderSet;
	import org.hive.events.LoaderSetEvent;
	
	import SWFAddress;
	import SWFAddressEvent;
	
	public class NavView extends Sprite
	{
		
		private var home_btn:MovieClip;
		private var about_btn:MovieClip;
		private var contact_btn:MovieClip;
		
		private var buttonArray:Array = [];
		
		private var _message:TextField;
				
		public function NavView()
		{
			
			SWFAddress.addEventListener(SWFAddressEvent.CHANGE, swfAddressChangeHandler);
			
			setupButtons();
			createMessage();
		}		
		
		private function setupButtons():void
		{
			home_btn = new MovieClip;			
			about_btn = new MovieClip;	
			contact_btn = new MovieClip;				
						
			var contactRollOver:MovieClip = new MovieClip();
			var contactRollOut:MovieClip = new MovieClip();
			
			contactRollOver.name = "contactRollOver";
			contactRollOut.name = "contactRollOut";
									
			contactRollOver.addChild( new Bitmap( LoadManager.getLoaderByName( "navContactRollOver" ).content.bitmapData, "auto", true)	);
			contactRollOut.addChild( new Bitmap( LoadManager.getLoaderByName( "navContactRollOut" ).content.bitmapData, "auto", true)	);
																
			contactRollOver.mouseEnabled = false;
			contactRollOut.mouseEnabled = false;
			contactRollOver.mouseChildren = false;
			contactRollOut.mouseChildren = false;
			
			contact_btn.addChild(contactRollOver);
			contact_btn.addChild(contactRollOut);
			contact_btn.addChild( createLabel("contact") );
			contact_btn.addChild( createHitArea("contact") );
						
			var homeRollOver:MovieClip = new MovieClip();
			var homeRollOut:MovieClip = new MovieClip();
			
			homeRollOver.name = "homeRollOver";
			homeRollOut.name = "homeRollOut";
			homeRollOver.mouseEnabled = false;
			homeRollOut.mouseEnabled = false;
			homeRollOver.mouseChildren = false;
			homeRollOut.mouseChildren = false;
			
			homeRollOver.addChild( LoadManager.getLoaderByName('navHomeRollOver'));
			homeRollOut.addChild( LoadManager.getLoaderByName('navHomeRollOut'));
						
			home_btn.addChild(homeRollOver);
			home_btn.addChild(homeRollOut);
			
			home_btn.addChild( createLabel("home") );
			home_btn.addChild( createHitArea("home") );
						
						
			var aboutRollOver:MovieClip = new MovieClip();
			var aboutRollOut:MovieClip = new MovieClip();
			
			aboutRollOver.name = "aboutRollOver";
			aboutRollOut.name = "aboutRollOut";
		
			aboutRollOver.addChild( new Bitmap( LoadManager.getLoaderByName( "navAboutRollOver" ).content.bitmapData, "auto", true)	);
			aboutRollOut.addChild( new Bitmap( LoadManager.getLoaderByName( "navAboutRollOut" ).content.bitmapData, "auto", true)	);
			
			aboutRollOut.mouseEnabled = false;
			aboutRollOver.mouseEnabled = false;
			aboutRollOut.mouseChildren = false;
			aboutRollOver.mouseChildren = false;
						
			about_btn.addChild(aboutRollOver);
			about_btn.addChild(aboutRollOut);
			about_btn.addChild( createLabel("about") );
			about_btn.addChild( createHitArea("about") );
															
			home_btn.mouseEnabled = false;
			about_btn.mouseEnabled = false;
			contact_btn.mouseEnabled = false;
			
			home_btn.x = 50;
			about_btn.x = home_btn.x + home_btn.width + 10;
			contact_btn.x = about_btn.x + about_btn.width + 10;
			
			buttonArray = [ 
							home_btn,
							about_btn,
							contact_btn
							]
				
					
							
			// HOME
			home_btn.getChildByName("homeRollOver").visible = false; 
			home_btn.getChildByName("homeRollOut").visible = true;
		
			//CONTACT
			contact_btn.getChildByName("contactRollOver").visible = false;
			contact_btn.getChildByName("contactRollOut").visible = true;
						
			// about
			about_btn.getChildByName("aboutRollOver").visible = false;
			about_btn.getChildByName("aboutRollOut").visible = true;
		
			addChild(home_btn);
			addChild(about_btn);
			addChild(contact_btn);

		
		}
		
		private function createMessage():void
		{
			_message = new TextField();
						
			var format:TextFormat = new TextFormat();
			format.color = 0xFFFFFF;
			format.size = 14;
			format.bold = true;
			format.font = "Helvetica";
			
			_message.embedFonts = true;
			_message.autoSize = TextFieldAutoSize.LEFT;
			_message.antiAliasType = AntiAliasType.ADVANCED;
			_message.defaultTextFormat = format;
			_message.selectable = false;
			_message.mouseEnabled = false;
			
			_message.x = 20;
			_message.y = 420;
			
			_message.text = "This is a demo using HIVE and swfaddress"; 
			
			addChild(_message);
		}
		
		private function createLabel( myName:String ):TextField
		{
			var myTextField:TextField = new TextField();
						
			var format:TextFormat = new TextFormat();
			format.color = 0x000000;
			format.size = 18;
			format.bold = true;
			format.font = "Helvetica";
			
			myTextField = new TextField();
			myTextField.embedFonts = true;
			myTextField.autoSize = TextFieldAutoSize.CENTER;
			myTextField.antiAliasType = AntiAliasType.ADVANCED;
			myTextField.defaultTextFormat = format;
			myTextField.selectable = false;
			myTextField.mouseEnabled = false;
			
			myTextField.text = myName.toUpperCase();	
			
			myTextField.x = 2;
			myTextField.y = 5;
						
			return myTextField;
		}
	
		
		private function createHitArea( myName:String ):MovieClip
		{
			var hitAreaMovieClip:MovieClip = new MovieClip();
				
			hitAreaMovieClip.graphics.beginFill(0xFFFFFF,0);
			hitAreaMovieClip.graphics.drawRect(0,0,115,26);
			hitAreaMovieClip.graphics.endFill();
			
			//hitAreaMovieClip.alpha = 0;
			hitAreaMovieClip.name = myName + "HitAreaMovieClip";
			hitAreaMovieClip.buttonMode = true;
			hitAreaMovieClip.mouseEnabled = true;
			
			hitAreaMovieClip.addEventListener(MouseEvent.CLICK, handleClick, false, 0, true);
			hitAreaMovieClip.addEventListener(MouseEvent.ROLL_OVER, handleRollOver, false, 0, true);
			hitAreaMovieClip.addEventListener(MouseEvent.ROLL_OUT, handleRollOut, false, 0, true);
			
			return hitAreaMovieClip;
		}

		
		private function handleClick(e:MouseEvent):void 
		{
					
						
			var newMsg:String;
			
			switch ( String(e.target.name) )
			{
				case "homeHitAreaMovieClip" :
					SWFAddress.setValue('home');
					newMsg = "\n MouseEvent home set";
				break;
				case "aboutHitAreaMovieClip" :
					SWFAddress.setValue('about');
					newMsg = "\n MouseEvent about set";
				break;
				case "contactHitAreaMovieClip" :
					SWFAddress.setValue('contact');
					newMsg = "\n MouseEvent contact set";
				break;
					
			}
			
			_message.textColor = 0xFFFFFF;
			_message.appendText( newMsg );
					
			
		}
		
		/*
		
		var value = SWFAddress.getValue();
		
		if (value == '/') 
		{
			// base 
		} else {
			// show error page not found
		}
		var title = 'SWFAddress Website';	
		var names = SWFAddress.getPathNames();
		for (var i = 0; i < names.length; i++) {
			title += ' / ' + names[i].substr(0,1).toUpperCase() + names[i].substr(1);
		}
		SWFAddress.setTitle(title);
		
		
		*/
		
		
		private function swfAddressChangeHandler(e:SWFAddressEvent):void 
		{
		  
		  	var address:String  = SWFAddress.getValue() as String;
		  	
		  	address = address.toLowerCase();
		  			  	
		  	var newMsg:String; 
		  	newMsg = "\n SWFAddressEvent change browser address: " + String(address);
		  	_message.appendText( newMsg );
		  			  			  	
		  	switch(address)
		  	{
			  	case "/" :
			  	case "/home" :
					nextState("com.brandonflowers.hiveswfaddressdemo.states.HomeState");	
			  		break;
			  	case "/about" :
					nextState("com.brandonflowers.hiveswfaddressdemo.states.AboutState");
			  		break;
			  	case "/contact" :
					nextState("com.brandonflowers.hiveswfaddressdemo.states.ContactState");
			  		break;
		  	}
		  	
		  	//address = "/tiger";
		  	
		  	if (address == "/home" || address == "/about" || address == "/contact" || address == "/" )
		  	{
		  		//nextState("com.brandonflowers.hiveswfaddressdemo.states.ErrorState");
		  		 _message.textColor = 0xFFFFFF;
		  		SWFAddress.setTitle("DEMO : " + address.substring(1) );
		  	} else 	{
		       	_message.textColor = 0xFF0000;
		  		_message.text = "oh snap! this page does not exist: " + address;	
		   }
		}
		
		private function handleRollOut(e:MouseEvent):void
		{
			//trace (" ROLLOUT MovieClip(event.target).name " +  MovieClip(e.target).name);
			
			switch ( String(e.target.name) )
			{
				case "contactHitAreaMovieClip" :
					contact_btn.getChildByName("contactRollOver").visible = false;
					contact_btn.getChildByName("contactRollOut").visible = true;
					break;
				case "aboutHitAreaMovieClip" : 
					about_btn.getChildByName("aboutRollOver").visible = false;
					about_btn.getChildByName("aboutRollOut").visible = true;
					break;
				case "homeHitAreaMovieClip" : 
					home_btn.getChildByName("homeRollOver").visible = false;	
					home_btn.getChildByName("homeRollOut").visible = true;	
					break;
			}
		}
		
		private function handleRollOver(e:MouseEvent):void
		{
						
			//trace (" ROLLOVER MovieClip(event.target).name " +  MovieClip(e.target).name);
								
			switch ( String(e.target.name) )
			{
				case "contactHitAreaMovieClip"  :
					contact_btn.getChildByName("contactRollOver").visible = true;
					contact_btn.getChildByName("contactRollOut").visible = false;
					break;
				case "aboutHitAreaMovieClip"  : 
					about_btn.getChildByName("aboutRollOver").visible = true;
					about_btn.getChildByName("aboutRollOut").visible = false;
					break;
				case "homeHitAreaMovieClip" : 
					home_btn.getChildByName("homeRollOver").visible = true;	
					home_btn.getChildByName("homeRollOut").visible = false;	
					break;
			}
									
		}

		

		private var homeState:HomeState = null;
		private var aboutState:AboutState = null;
		private var contactState:ContactState = null;
	
		
		private function nextState(stateClassName:String):void
		{
			
			trace ("nextState(stateClassName: " + stateClassName);
			var nextStateSequence:ActionSequence = new ActionSequence( "Next State " + stateClassName );
			var ClassReference:Class = getDefinitionByName(stateClassName) as Class;
           				
			nextStateSequence.push( new SequenceFunction(StateManager, StateManager.switchState, new ClassReference() ));
			StateManager.playSequence(nextStateSequence);
		
		}
	

			
	}
	
}